package com.example.campusrear.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.campusrear.Service.*;
import com.example.campusrear.Utils.R;
import com.example.campusrear.entity.*;
import com.example.campusrear.entity.Class;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/grade")
public class gradeController {
    @Resource
    private ClassGradeService classGradeService;

    @Resource
    private StudentService studentService;

    @Resource
    private ClassService classService;

    @Resource
    private CourseCategoryService courseCategoryService;

    //查询某个班级的成绩

    @GetMapping("/queryClassGrade")
    public R<Page<ClassGradeDto>> queryClassGrade(int page,int pageSize,Long classId,Long courseId,String examTime,String type){
        Page<ClassGrade> classGradePage = new Page<>(page,pageSize);
        Page<ClassGradeDto> gradeDtoPage = new Page<>();
        LambdaQueryWrapper<ClassGrade> wrapper = new LambdaQueryWrapper<>();
        if (classId!=null){
            wrapper.eq(ClassGrade::getClassId,classId);
        }
        if (courseId!=null){
            wrapper.eq(ClassGrade::getCourseCapId,courseId);
        }
        if (examTime!=null && !examTime.equals("undefined")){
            wrapper.like(StringUtils.isNotEmpty(examTime),ClassGrade::getExamTime,examTime);
        }
        if (type!=null && !type.equals("undefined")){
            wrapper.like(StringUtils.isNotEmpty(type),ClassGrade::getGradeType,type);
        }
        classGradeService.page(classGradePage,wrapper);
        BeanUtils.copyProperties(classGradePage,gradeDtoPage,"records");
        List<ClassGrade> records = classGradePage.getRecords();
        List<ClassGradeDto> gradeDtoPages = records.stream().map(item->{
            ClassGradeDto gradeDto = new ClassGradeDto();
            BeanUtils.copyProperties(item,gradeDto);
            Long studentId = gradeDto.getStudentId();
            Long classId1 = gradeDto.getClassId();
            Long courseCapId = gradeDto.getCourseCapId();
            Student student = studentService.getById(studentId);
            Class aClass = classService.getById(classId1);
            CourseCategory courseCategory = courseCategoryService.getById(courseCapId);
            gradeDto.setCName(aClass.getClassName());
            gradeDto.setSName(student.getStudentName());
            gradeDto.setCPName(courseCategory.getCourseName());
            return gradeDto;
        }).collect(Collectors.toList());
        gradeDtoPage.setRecords(gradeDtoPages);
        return R.success(gradeDtoPage);
    }

    //添加某个学生成绩
    @PostMapping("/saveGrade")
    public R<String> saveGrade(@RequestBody ClassGrade grade){
        classGradeService.save(grade);
        return R.success("保存成功");
    }
    @PostMapping("/upload")
    public R<String> upload(@RequestBody ClassGrade grade){
        classGradeService.updateById(grade);
        return R.success("修改成功");
    }
    @DeleteMapping("/delete/{gId}")
    public R<String> delete(@PathVariable Long gId){
        boolean b = classGradeService.removeById(gId);
        if (b){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }
}
